package aiingames.simulation.agent;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import aiingames.simulation.agent.AgentAction.SimpleAction;

public class State implements Serializable, Comparable<State> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final String signature;
	private double value = 0;
	private Map<SimpleAction, State> successors  = new HashMap<SimpleAction, State>();
	private SimpleAction lastAction;

	public State(String stateSignature) {
		this.signature = stateSignature;
		//successors;
//		successors.put(SimpleAction.LEFT, null);
//		successors.put(SimpleAction.STOP, null);
//		successors.put(SimpleAction.RIGHT, null);
	}

	@SuppressWarnings("unchecked")
	public State(State oldState) {
		//System.out.println(oldState);
		this.signature =oldState.getSignature();
		Map newSuccessors =new HashMap<SimpleAction, State>();//  = new HashMap(oldState.getSuccessors());
		for (Map.Entry<SimpleAction, State> entry : oldState.getSuccessors().entrySet())
		{
		    System.out.println("new:" + entry.getKey() + "/" + entry.getValue());
		    newSuccessors.put(entry.getKey(), entry.getValue());
		}


		//for(HashMap<SimpleAction,State> e:l) newSuccessors.put(e.getKey().clone(), e.getValue().clone());
		this.successors = newSuccessors;
		
		System.out.println("copied:" + newSuccessors.size());
		System.out.println(newSuccessors);

		this.value = new Double(oldState.getValue());

		//this.signature = stateSignature;
		//successors;
//		successors.put(SimpleAction.LEFT, null);
//		successors.put(SimpleAction.STOP, null);
//		successors.put(SimpleAction.RIGHT, null);
	}
	
	
	public String getSignature() {
		return signature;
	}

	public String toString() {
		return this.signature + "| v: " + this.value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public double getValue() {
		return value;
	}

	public Map<SimpleAction,State> getSuccessors() {
		return successors;
	}

	public void setLastAction(SimpleAction lastAction) {
		this.lastAction = lastAction;
	}

	public SimpleAction getLastAction() {
		return lastAction;
	}

	@Override
	public int compareTo(State o) {
		if (o.signature.equals(signature)) {
			return 0;
		}
		return -1;
	}

	public void setSuccessor(SimpleAction simpleAction, State stateBeforeAction) {
		// TODO Auto-generated method stub
		System.out.println("state SUCCESSOR "+stateBeforeAction);
		successors.put(simpleAction, stateBeforeAction);
		
		System.out.println("successors size "+successors.size());
		
	}
}